import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const body = await readBody(event);

  // 验证必填字段
  const requiredFields = [
    'serviceId',
    'contextId',
    'bookingDate',
    'timeSlot',
    'customerName',
    'phoneNumber',
  ];
  for (const field of requiredFields) {
    if (!body[field]) {
      throw createError({
        statusCode: 400,
        statusMessage: `缺少必填字段: ${field}`,
      });
    }
  }

  // 生成预约记录
  const booking = {
    id: `booking_${Date.now()}`,
    bookingNo: `BK${new Date().getFullYear()}${String(Date.now()).slice(-6)}`,
    serviceId: body.serviceId,
    serviceName: '温泉泡浴', // 这里应该根据serviceId查询实际服务名称
    contextId: body.contextId,
    contextName: '温泉洗浴', // 这里应该根据contextId查询实际场景名称
    customerName: body.customerName,
    phoneNumber: body.phoneNumber,
    wechatId: body.wechatId || '',
    memberCard: body.memberCard || '',
    bookingDate: body.bookingDate,
    timeSlot: body.timeSlot,
    status: 'pending', // 初始状态为待确认
    remarks: body.remarks || '',
    createTime: new Date().toISOString(),
    updateTime: new Date().toISOString(),
  };

  // 模拟数据库存储
  console.log('创建预约记录:', booking);

  return useResponseSuccess(booking);
});
